package com.xnyzc.lhy.resource.controller.invoice;

import com.baiwang.bop.request.impl.invoice.impl.FormatfileQueryRequest;
import com.xnyzc.lhy.common.exception.InvoiceException;
import com.xnyzc.lhy.resource.entity.invoice.*;
import com.xnyzc.lhy.resource.service.invoice.IInvoiceService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 电子发票接口
 * </p>
 *
 * @author haoyfa
 * @since 2019-10-24
 */
@Slf4j
@RestController
@RequestMapping("/invoice")
public class InvoiceController {

    @Autowired
    private IInvoiceService invoiceService;

    /**
     * 发票开具
     *
     */
    @ApiOperation(value = "发票开具", notes = "发票开具")
    @PostMapping(value = "invoiceOpenRequest")
    public InvoiceOpenResult invoiceOpenRequest(@ApiParam(name = "invoiceParam", value = "开发票信息") @RequestBody InvoiceParam invoiceParam) {
        InvoiceOpenResult invoiceOpenResult = null;
        try {
            invoiceOpenResult = invoiceService.invoiceOpenRequest(invoiceParam);
        }
        catch (InvoiceException invoiceException)
        {
            log.error("百望云发票接口服务错误");
            invoiceOpenResult = new InvoiceOpenResult();
            invoiceOpenResult.setIsSuccess(false);

            //返回错误信息
            InvoiceErrorResponse  invoiceErrorResponse = new InvoiceErrorResponse(
                    invoiceException.getErrno().getDesc(),
                    invoiceException.getErrmsg());
            invoiceOpenResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return invoiceOpenResult;
    }

    /**
     * 发票作废
     */
    @ApiOperation(value = "发票作废", notes = "发票作废")
    @PostMapping(value = "invoiceInvalidRequest")
    public ReInvoiceInvalidResult invoiceInvalidRequest(@ApiParam(name = "invoiceInvalidParam", value = "发票作废") @RequestBody InvoiceInvalidParam invoiceInvalidParam){
        ReInvoiceInvalidResult reInvoiceInvalidResult = null;
        try {
            reInvoiceInvalidResult = invoiceService.invoiceInvalidRequest(invoiceInvalidParam);
        }
        catch (InvoiceException invoiceException)
        {
            log.error("百望云发票作废服务错误");
            reInvoiceInvalidResult = new ReInvoiceInvalidResult();
            reInvoiceInvalidResult.setIsSuccess(false);

            //返回错误信息
            InvoiceErrorResponse  invoiceErrorResponse = new InvoiceErrorResponse(
                    invoiceException.getErrno().getDesc(),
                    invoiceException.getErrmsg());
            reInvoiceInvalidResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return reInvoiceInvalidResult;
    }

    /**
     * 板式文件生成
     */
    @ApiOperation(value = "板式文件生成", notes = "板式文件生成")
    @PostMapping(value = "formatfileBuildRequest")
    public InvoiceFormatFileResult formatfileBuildRequest(@ApiParam(name = "invoiceFormatFileParam", value = "板式文件生成请求信息") @RequestBody InvoiceFormatFileParam invoiceFormatFileParam){
        InvoiceFormatFileResult invoiceFormatFileResult = null;
        try {
            invoiceFormatFileResult = invoiceService.formatfileBuildRequest(invoiceFormatFileParam);
        }
        catch (InvoiceException invoiceException)
        {
            log.error("百望云板式文件生成服务错误");
            invoiceFormatFileResult = new InvoiceFormatFileResult();
            invoiceFormatFileResult.setIsSuccess(false);

            //返回错误信息
            InvoiceErrorResponse  invoiceErrorResponse = new InvoiceErrorResponse(
                    invoiceException.getErrno().getDesc(),
                    invoiceException.getErrmsg());
            invoiceFormatFileResult.setInvoiceErrorResponse(invoiceErrorResponse);
        }

        return invoiceFormatFileResult;
    }

    /**
     * 板式文件查询
     */
    @ApiOperation(value = "板式文件查询", notes = "板式文件查询")
    @PostMapping(value = "formatfileQueryRequest")
    public void formatfileQueryRequest(FormatfileQueryRequest request){}
}
